package com.xiaoq.sites.endpoint;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.sites.entity.SitesPointEntity;
import com.xiaoq.sites.entity.SitesRouteEntity;
import com.xiaoq.sites.service.ISitesService;
import com.xiaoq.sites.vo.RouteDetailVo;
import com.xiaoq.sites.vo.SitesPoint2Vo;

@RestController
@RequestMapping("/api/v1")
@Api(tags = "线路C端相关Api")
public class RouteEndpoint {
	
	private Logger logger = LoggerFactory.getLogger(RouteEndpoint.class);
	
	private static final class RouteListResponse extends GenericListResponse<RouteDetailVo> {}; 
	
	@Autowired
	private ISitesService msSitesApi;
	
	@ApiOperation(value="查询线路列表", notes="根据景区ID查询线路列表")
	@ApiResponses({
		@ApiResponse(code = 200, response = RouteListResponse.class, message = "查询线路列表")
	})
	@GetMapping(value="sites/{sitesId}/sites_route", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	public ResponseEntity<?> routeList( @PathVariable(value ="sitesId")String sitesId) {
		String errorMsg = "查询线路列表出错";
		try {
			List<SitesRouteEntity> sitesRouteList = msSitesApi.findSitesRouteListBySitesId(sitesId);
			List<RouteDetailVo> list = new ArrayList<RouteDetailVo>();
			RouteDetailVo routeDetailVo = null;
			SitesPoint2Vo sitesPointVo = null;
			List<SitesPoint2Vo> sitesPointVoList = null;
			
			List<String> sitesRouteIdList = null;
			if(CollectionUtils.isNotEmpty(sitesRouteList)) {
				
				sitesRouteIdList = sitesRouteList.stream().map(sitesRoute -> sitesRoute.getId()).collect(Collectors.toList());
				Map<String, List<SitesPointEntity>> routeId2SitesPointMap = msSitesApi.findSitesPointMapByRouteIdList(sitesRouteIdList);
				
				for (SitesRouteEntity sitesRoute : sitesRouteList) {
					routeDetailVo = new RouteDetailVo();
					BeanUtils.copyProperties(sitesRoute, routeDetailVo);
					
					List<SitesPointEntity> sitesPointEntitiesList = routeId2SitesPointMap.get(sitesRoute.getId());
					if(CollectionUtils.isNotEmpty(sitesPointEntitiesList)) {
						sitesPointVoList = new ArrayList<>();
						for (SitesPointEntity sitesPointEntity : sitesPointEntitiesList) {
							sitesPointVo = new SitesPoint2Vo();
							BeanUtils.copyProperties(sitesPointEntity, sitesPointVo);
							sitesPointVoList.add(sitesPointVo);
						}
					}
					
					routeDetailVo.setSitesPoints(sitesPointVoList);
					
					list.add(routeDetailVo);
				}
			}
			
			return RouteListResponse.listNoCount(list);
		} catch (Exception e) {
			logger.error(errorMsg,e);
			return GenericResponse.ng(errorMsg);
		}
	}
	
}
